<?php
namespace Home\Controller;

use Think\Controller;
use Think\Log;
use Think\Page;

class UserController extends Controller
{
    public function index()
    {
//        G('begin');
        $user = D('User');
        $list = $user->select();
        //手工抛出错误
        //E('错误!');
//        echo $user->getLastSql();
//        $user->query('SELECT * FROM bbb');
//        echo $user->getDbError();

        //Log::record('手动日志', 'WARN', true);
        //强制写入日志
        //Log::write('手工日志', 'WARN');

        //调试方法
        //trace($list,'提示','user');

        //变量调试
        //dump($list);
        $this->assign('list', $list);
        $this->display();
//        G('end');
//
//        echo G('begin','end'). 's';
//        echo '<br/>';
//        echo G('begin','end', 'm'). 'kb';

        //echo C('DATA_CACHE_TYPE');
        //数据缓存
        /*if (!$list = S('list')) {

            $user = D('User');
            $list = $user->select();
            S('list', $list, 10);
        }
            S('list',null);*/
        //快速缓存
        /*if (!$list = F('list')) {

            $user = D('User');
            $list = $user->select();
            F('list', $list, TEMP_PATH);
        }*/

//        $user = D('User');
//        $list = $user->cache(true)->select();
//        $list = $user->cache('cache_user', 10)->select();
//        var_dump(S('cache_user'));
//        S('cache_user', null);
//        $list = $user->select();
//        $this->assign('list', $list);
//        $this->display();


        //数据分页
        /*$user = D('User');
        $count = $user->count();
        $page = new Page($count, 1);
        $page->setConfig('first', '首页');
        $page->setConfig('prev', '上一页');
        $page->setConfig('next', '下一页');
        $page->setConfig('last', '末页');
        $page->setConfig('theme', '共%TOTAL_ROW%条数据 %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $show = $page->show();
        $list = $user->limit($page->firstRow, $page->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();*/

        /*$user = D('UserView');
        echo '<pre>';
        print_r($user->group('id')->select());
        echo '</pre>';*/


        /*echo '<pre>';
        $user = D('User');
        $arr = $user->relation(true)->select();
        print_r($arr);
        echo '</pre>';*/
    }

    /*    public function add()
        {
            $user = D('User');
            $data['user'] = '测试用户1';
            $data['email'] = 'ceshi@qq.com';
            $data['Card'] = array(
                'code' => '3209xx',
            );
            $user->relation(true)->add($data);
        }

        public function delete()
        {
            $user = D('User');
            $user->relation(true)->delete(16);
        }

        public function save()
        {
            $user = D('User');
            $data['user'] = '测试用户2';
            $data['email'] = 'ceshi2@qq.com';
            $data['Card'] = array(
                'code' => '3209zz',
            );
            $user->relation(true)->where(array('id' => 17))->save($data);
        }*/


//    public function index()
//    {
////        L('name','Lee');
////        echo L('name');
////        echo L('_CONTROLLER_NOT_EXIST_');
//        $this->display();
//    }
//
//    public function lang()
//    {
//        setcookie('think_language', 'en-us');
//    }

//    public function upload()
//    {
//        /*$config = array(
//            'maxSize' => 3000000,
//            'exts' => array('jpg','jpeg','gif','png'),
//            'savePath' => './',
//        );*/
//        //实例化上传类
//        $upload = new Upload();
//        $upload->maxSize = 3000000;
//        $upload->exts = array('jpg', 'jpeg', 'gif', 'png');
//        $upload->savePath = './';
////        $upload->saveName = '123';
////        $upload->replace = true;
////        $upload->autoSub = false;
////        $upload->subName = 'abc';
////        $upload->subName = array('date','Ymd');
////        $upload->subName = 'get_user_id';
//        $upload->hash = false;
//        $info = $upload->upload();
//        if (!$info) {
//            $this->error($upload->getError());
//        } else {
//            //$this->success('上传成功!');
//            foreach ($info as $file) {
////                echo $file['savepath'] . $file['savename'] . '<br/>';
//                echo '<pre>';
//                print_r($file);
//                echo '</pre>';
//            }
//        }
//
//    }


    /* public function all($id)
     {
         echo '显示所有用户列表' . $id;
     }*/

    /* public function test()
     {
         //$userEvent = new UserEvent();
         $userEvent = A('User', 'Event');   //A('Admin/User','Event')
         $userEvent->test();
         echo 'test';
     }*/
    /*
        //_before_[方法名]
        public function _before_index(){
            echo '在之前执行';
        }
        //_after_[方法名]
        public function _after_index(){
            echo '在之后执行';
        }*/

//    public function select()
//    {
//        //layout(true);
//        layout('Public/layout');
//        layout(false);
//        $user = '蜡笔小新';
//
//        $this->assign('user', $user);
//
//        $this->display();
//    }


//    public function test($user, $pass)
//    {
//        echo 'user:' . $user . '<br/>pass:' . $pass;
//
//    }
//
//    public function model()
//    {
//
//        //字段映射
//        $user = D('User');
//        var_dump($user->create());
//
//        /**
//         * Model方法之前的代码
//         */
//        /*创建Model基类 ，传递User表,think_user
//            $user = new Model('User','tp_');
//            $user = new Model('User','think_','mysql://root:lj8229797@localhost/thinkphp');
//            var_dump($user);
//            $user = new Model('User');
//            $user = M('User');
//            $user = new Model('User');
//            var_dump($user->select());
//            $user = new UserModel();
//            var_dump($user->select());
//            $user = D('User');
//            $user = M();
//            var_dump($user->query("SELECT * FROM think_user where user='蜡笔小新'"));
//            var_dump($user->getDbFields());
//            var_dump($user->select());
//            $user = M('User');
//            var_dump($user->where('id=1 and user="蜡笔小新"')->select());
//            $condition['id']=1;
//            $condition['user']='蜡笔小新';
//            $condition['_logic']='OR';
//            $condition = new \stdClass();
//            $condition->id = 1;
//            $condition->user = '蜡笔小新';
//            $condition->_logic = 'OR';
//            var_dump($user->where($condition)->select());
//            $user = M('User');
//            $map['id']=array('eq',1); 等于
//            $map['id']=array('neq',1);  不等于
//            $map['id']=array('gt',1);  大于
//            $map['id']=array('egt',1);  大于等于
//            $map['id']=array('lt',1);  小于
//            $map['id']=array('elt',1);  //小于等于
//            $map['user']=array('like','%小%');  like
//            $map['user']=array('notlike','%小%');   NOT LIKE
//            $map['user']=array('notlike',array('%小%','%蜡%','and'));
//            $map['id']=array('between','1,3');
//            $map['id']=array('between',array('1','3'));
//            $map['id']=array('not between','1,3');
//            $map['id']=array('in',array('1','3','4'));
//            $map['id']=array('not in',array('1','2','4'));
//            $map['id']=array('exp','in ("1","2","3")');
//            $map['id']=array('exp','=1 AND user="蜡笔小新"');
//            $map['id']=array('exp','=1');
//            $map['user']=array('exp',"='蜡笔小新'");
//            $map['_logic']='OR';
//            var_dump($user->where($map)->select());
//            $user = M('User');
//            $map['user|email']='蜡笔小新';
//            $map['user&email']='蜡笔小新';
//            $map['id&user']=array('1','蜡笔小新','_multi'=>true);
//            $map['id&user']=array(array('gt','0'),'蜡笔小新','_multi'=>true);
//            $map['id']=array(array('gt','1'),array('lt','4'));
//            $map['id']=array(array('gt','1'),array('lt','4'),'OR');
//            $map['id'] = array('eq', '1');
//            $map['_string']='user="蜡笔小新" AND email="xiaoxin@163.com"';
//            $map['_logic']="OR";
//            $map['_query']='user=蜡笔小新&email=xiaoxin@163.com';
//            $where['id'] = '2';
//            $map['_complex'] = $where;
//            $map['_logic']="OR";
//            var_dump($user->count());
//            var_dump($user->count('email'));
//            var_dump($user->max('id'));
//            var_dump($user->min('id'));
//            var_dump($user->avg('id'));
//            var_dump($user->sum('id'));
//            print_r($user->getByUser('蜡笔小新'));
//            print_r($user->getFieldByUser('蜡笔小新','id'));
//            print_r($user->execute("UPDATE think_user SET user='蜡笔小新' where id='1'"));
//            */
//        /*echo '<pre>';
//
//                $user = M('User');
//                var_dump($user->where('id>1')->order('date DESC')->limit('2')->select());
//                var_dump($user->select(array('where'=>array('id'=>array('neq','1')),'order'=>'date DESC','limit'=>'2')));
//                var_dump($user->where('id!=1')->order('date DESC')->limit('2')->select());
//                var_dump($user->where('id in (1,2,3,4)')->find());   只显示一条
//                var_dump($user->where('id=5')->delete());   删除信息
//                $map['id'] = 1;
//                var_dump($user->where($map)->select());
//                $map['id'] = array('gt','1');
//                $map['_logic']='OR';
//                print_r($user->where($map)->where('user="蜡笔小新"')->select());
//                var_dump($user->order(array('id'=>'DESC'))->select());
//                var_dump($user->order('id DESC,email DESC')->select());
//                var_dump($user->field('id,user')->select());
//                var_dump($user->field(array('id','LEFT(user,3) AS left_user'))->select());
//                var_dump($user->field(array('id','LEFT(user,3)'=>'left_user'))->select());
//                var_dump($user->field()->select());
//                var_dump($user->field('user,email')->create());
//                var_dump($user->limit(4,2)->select());
//                var_dump($user->page(2,2)->select());
//                var_dump($user->table('think_info')->select());
//                var_dump($user->table('__USER__')->select());
//                var_dump($user->table('__INFO__')->select());
//                var_dump($user->field('a.id,a.user,b.id,b.user')->table('__USER__ a,__INFO__ b')->select());
//                var_dump($user->field('a.id,a.user,b.id,b.user')->table(array('think_user'=>'a','think_info'=>'b'))->select());
//                var_dump($user->alias('a')->select());
//                var_dump($user->field('user,SUM(id)')->group('id')->select());
//                var_dump($user->field('user,SUM(id)')->group('id')->having('id>2')->select());
//                var_dump($user->comment('查找所有用户')->select());
//                var_dump($user->join('think_user ON think_info.id=think_user.id')->select());
//                var_dump($user->join('think_info ON think_info.id=think_user.id','right')->select());
//                var_dump($user->field('user')->distinct(true)->select());
//                var_dump($user->cache(true)->select());
//                var_dump($user->select());
//
//                $user = D('User');
//
//                var_dump($user->scope('sql1')->select());
//                var_dump($user->scope('sql2')->select());
//                var_dump($user->scope('sql1')->scope('sql2')->select());
//                var_dump($user->scope()->select());
//                var_dump($user->scope('sql2',array('limit'=>'4'))->select());
//                var_dump($user->scope(array('where'=>array('id'=>'1'),'order'=>'date ASC','limit'=>'2'))->select());
//                var_dump($user->sql2()->select());
//
//
//                echo '</pre>';*/
//
//    }
//
//    public function create()
//    {
//        echo "<pre>";
//
//
////        $user = M('User');
////        $data['user']='樱桃小丸子';
////        $data['email']='yintao@qq.com';
////        $data['user'] = $_POST['user'];
////        $data['email'] = $_POST['email'];
////        $data['date'] = date("Y-m-d H:i:s");
////        $data = new \stdClass();
////        $data->user = $_POST['user'];
////        $data->email = $_POST['email'];
////        $data->date = date("Y-m-d H:i:s");
//
////        var_dump($user->create($_GET));
////        var_dump($user->create($_POST,Model::MODEL_INSERT));
////        var_dump($user->create($_POST,Model::MODEL_UPDATE));
//
////        合法字段
////        var_dump($user->field('user')->create());
////        再模型是限制字段
//        //$user = D('user');
//        //var_dump($user->create());
//
//        echo "</pre>";
//    }
//
//    public function add()
//    {
//        $rules = array(
//            array('user', 'sha1', 3, 'function'),
//        );
//        $user = M('User');
//        $data['user'] = '蜡笔小新';
//        if ($user->auto($rules)->create($data)) {
//            $user->add();
//        } else {
//
//        }
//
//        /*$user = D('User');
//        $data['id'] = 14;
//        $data['user'] = '';
//        $data['email'] = 'bnbbs@163.com';
//        if ($user->create($data)) {
////            $user->add();
//            $user->save();
//        } else {
//
//        }*/
//
////
////        $user = D('User');
////        $data['user'] = '李炎1111111';
////        $data['email'] = '李炎1111';
//////        $data['name'] = '李炎会';
////        if ($user->create($data)) {
////            echo '所有字段验证成功';
////        }else{
////            var_dump($user->getError());
////            //$this->ajaxReturn($user->getError());
////        }
//
////        $rules = array(
////            array('user','require','用户名不得为空'),
////        );
////        $user = M('User');
////        $data['user'] = '123';
////        if ($user->validate($rules)->create($data)) {
////            echo '所有字段验证成功';
////        }else{
////            var_dump($user->getError());
////        }
//
//
////        $user = M('User');
////        $data['user']='李炎会';
////        $data['email']='yc@163.com';
////        $data['date']=date("Y-m-d H:i:s");
////        $user->add($data);
//
////        $user = M('User');
////        $data = $user->create();
////        $data['date']=date("Y-m-d H:i:s");
////        $data = 'user=星矢&email=xinshi@qq.com&date='.date("Y-m-d H:i:s");
////        $user->data($data)->add();
//    }
//
//    public function select()
//    {
//        echo "<pre>";
//
//        //$user = M('User');
////        var_dump($user->select());
////        var_dump($user->find());
////        var_dump($user->order(array('id'=>'DESC'))->find());
////        var_dump($user->getField('user'));
////        var_dump($user->getField('user',true));
////        var_dump($user->getField('user,email'));   //重复的被屏蔽了
////        var_dump($user->getField('id,user,email',':'));
////        var_dump($user->getField('id,user,email',2));
////
//        echo "</pre>";
//    }
//
//    public function save()
//    {
//        echo "<pre>";
//
//        $user = M('User');
////        $data['user'] = '蜡笔大新';
////        $data['email'] = 'daxin@163.com';
////        $map['id'] = 1;
////        $user->where($map)->save($data);
////        $data['id'] = 1;
////        $data['user'] = '蜡笔老新';
////        $data['email'] = 'laoxin@qq.com';
////        $user->save($data);
////        $user->create();
////        echo $user->save();
//
////        $map['id'] = 1;
////        $user->where($map)->setField('user', '新新');
//        $map['id'] = 1;
////        $user->where($map)->setInc('count',1);
//        $user->where($map)->setDec('count', 1);
//
//        echo "</pre>";
//    }
//
//    public function delete()
//    {
//        echo "<pre>";
//
//
//        //$user = M('User');
////        $user->delete(8);
////        $map['id']='9';
////        $user->where($map)->delete();
////        $user->delete('10,11');
//        //$map['count'] = '0';
//        //var_dump($user->where($map)->order(array('date'=>'DESC'))->limit('1')->delete());
//
//
//        echo "</pre>";
//    }
//
//    public function ar()
//    {
//        echo "<pre>";
//
//        $user = M('User');
////        $user->user = '星矢';
////        $user->email = 'xingshi@qq.com';
////        $user->date = date('Y-m-d H:i:s');
////        $user->add();
////        var_dump($user->find(7));
////        var_dump($user->getByUser('新新'));
////        echo $user->email;
////        var_dump($user);
////        var_dump($user->select('1,2'));
////        $user->find(1);
////        $user->user='蜡笔小新';
////        $user->save();
////        $user->find(7);
////        $user->delete();
//
//        echo "</pre>";
//
//    }


}